//
//  YsNetWorkManage.m
//  YsCloudFileManageSDK
//
//  Created by 宋宏康 on 2020/2/24.
//

#import "YsNetWorkManage.h"
#import <AFNetworking/AFNetworking.h>
#import <YYKit/YYKit.h>
#import "YsCloudFileManagerHeader.h"

@implementation YsNetWorkManage
+ (NSURLSessionDataTask *)postHttpDataWithUrlStr:(NSString *)url dic:(NSDictionary *)dic successBlock:(SuccessBlock)successBlock failureBlock:(FailedBlock)failureBlock
{
    NSCParameterAssert(successBlock != nil);
    NSCParameterAssert(failureBlock != nil);
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    
    AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer]; // 请求参数
    [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager setRequestSerializer:requestSerializer]; // 请求格式是json
    
    AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializer];
    [responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    [manager setResponseSerializer:responseSerializer]; // 返回的格式是json
    
    
    NSURLSessionDataTask *task = [manager POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
           
       } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
           YsResultModel *model = [YsResultModel modelWithJSON:responseObject];
           if (model.isSuccessful) {
               successBlock(model);
           }else{
               NSError *error = [NSError errorWithDomain:@"cn.YsNetWorkManage.errormsg" code:100 userInfo:@{NSLocalizedDescriptionKey : model.errorMsg}];
            failureBlock(error);
           }
       } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
           /** 这里是处理事件的回调 */
           failureBlock(error);
       }];
       return task;
}

+ (NSURLSessionDataTask *)uploadFetchWayWithuid:(NSString *)uid
                                        success:(void (^)(YsResultModel *result))successfull
                                           fail:(void (^)(NSError *error))failed
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:uid forKey:@"uid"];
    NSURLSessionDataTask *task = [self postHttpDataWithUrlStr:kGetServerUrl dic:dict successBlock:successfull failureBlock:failed];
    return task;
}


+ (NSURLSessionDataTask *)uploadcheckWithuid:(NSString *)uid
                                    Withhash:(NSString *)hash
                                 WitfileName:(NSString *)fileName
                                WithfileSize:(long long)fileSize
                            WithmaxFileCount:(NSInteger)maxFileCount
                                    Withsize:(NSInteger)size
                                     success:(void (^)(YsResultModel *result))successfull
                                     fail:(void (^)(NSError *error))failed
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:uid forKey:@"uid"];
    [dict setValue:hash forKey:@"hash"];
    [dict setValue:fileName forKey:@"fileName"];
    [dict setValue:@(fileSize) forKey:@"fileSize"];
    [dict setValue:@(maxFileCount) forKey:@"maxFileCount"];
    [dict setValue:@(size) forKey:@"size"];
    
    NSURLSessionDataTask *task = [self postHttpDataWithUrlStr:kSuperUploadCheckTestUrl dic:dict successBlock:successfull failureBlock:failed];
    return task;
}

+ (NSURLSessionDataTask *)uploadMoveFileWithuid:(NSString *)uid
                                        Withname:(NSString *)name
                                        Withsize:(NSInteger)size
                                        Withhash:(NSString *)hash
                                        Withmime:(NSString *)mime
                                         success:(void (^)(YsResultModel *result))successfull
                                           fail:(void (^)(NSError *error))failed
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:uid forKey:@"uid"];
    [dict setValue:name forKey:@"name"];
    [dict setValue:@(size) forKey:@"size"];
    [dict setValue:hash forKey:@"hash"];
    [dict setValue:mime forKey:@"mime"];
    
    NSURLSessionDataTask *task = [self postHttpDataWithUrlStr:kSuperUploadFinishTestUrl dic:dict successBlock:successfull failureBlock:failed];
    return task;
}

@end
